What is just-clone?
The just-clone npm package is a utility for deep cloning JavaScript objects. It allows you to create a deep copy of an object, ensuring that nested objects and arrays are also cloned rather than referenced.
What are just-clone's main functionalities?
Deep Cloning of Objects
This feature allows you to create a deep copy of an object, ensuring that nested objects are also cloned.
const clone = require('just-clone');
const original = { a: 1, b: { c: 2 } };
const copied = clone(original);
console.log(copied); // { a: 1, b: { c: 2 } }
Deep Cloning of Arrays
This feature allows you to create a deep copy of an array, ensuring that nested arrays are also cloned.
const clone = require('just-clone');
const original = [1, [2, 3], 4];
const copied = clone(original);
console.log(copied); // [1, [2, 3], 4]
Handling of Complex Data Types
This feature ensures that complex data types like Date, RegExp, and Map are properly cloned.
const clone = require('just-clone');
const original = { a: new Date(), b: /regex/, c: new Map([[1, 'one']]) };
const copied = clone(original);
console.log(copied); // { a: Date, b: /regex/, c: Map(1) { 1 => 'one' } }
Other packages similar to just-clone
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. It includes a deep cloning function called `_.cloneDeep`, which offers similar functionality to just-clone but is part of a much larger library.
rfdc
rfdc (Really Fast Deep Clone) is a lightweight and fast deep cloning library. It is designed to be a minimalistic alternative to larger libraries like lodash, focusing specifically on deep cloning performance.
clone-deep
clone-deep is another utility for deep cloning objects and arrays. It is similar to just-clone in terms of functionality but offers additional options for customizing the cloning process.
just-clone
Part of a library of zero-dependency npm modules that do just do one thing.
Guilt-free utilities for every occasion.
🍦 Try it
npm install just-clone
yarn add just-clone
Deep copies objects, arrays, maps and sets
import clone from 'just-clone';
var arr = [1, 2, 3];
var subObj = { aa: 1 };
var obj = { a: 3, b: 5, c: arr, d: subObj };
var objClone = clone(obj);
arr.push(4);
objClone.d.bb = 2;
obj;
objClone;